<template>
  <y-dialog @close="emit('close')" width="1000px" append-to-body>
    <el-table
      :data="props.data"
      border
      style="width: 100%"
      max-height="800"
      row-key="id"
    >
      <el-table-column v-if="props.level === '1'" type="expand">
        <template #default="props">
          <div m="4">
            <el-table
              :data="props.row.steps.filter((obj) => obj.result !== '0')"
              border
            >
              <el-table-column label="序号" prop="id" width="80" />
              <el-table-column
                label="步骤"
                prop="step"
                :show-overflow-tooltip="true"
              />
              <el-table-column
                label="预期值"
                prop="exp"
                :show-overflow-tooltip="true"
              />
              <el-table-column
                label="实际值"
                prop="actual"
                :show-overflow-tooltip="true"
              >
                <template #default="scope">
                  <span>{{ scope.row.actual }}</span>
                </template>
              </el-table-column>
              <el-table-column
                label="测试结果"
                prop="result"
                align="center"
                width="80"
              >
                <template #default="scope">
                  <y-dict-tag
                    :options="tms_tc_result"
                    :value="scope.row.result"
                  />
                </template>
              </el-table-column>
              <el-table-column label="结果分析" prop="analysis" align="center">
                <template #default="scope">
                  <y-select
                    v-model="scope.row.analysis"
                    placeholder="请选择问题类型"
                    :options="tms_tc_analysis"
                  />
                </template>
              </el-table-column>
              <el-table-column label="备注" prop="remark" align="center">
                <template #default="scope">
                  <el-input
                    type="textarea"
                    v-model="scope.row.remark"
                  ></el-input>
                </template>
              </el-table-column>
            </el-table>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="序号" width="100" align="center" type="index" />
      <el-table-column
        label="用例编号"
        prop="number"
        align="center"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="用例名称"
        prop="name"
        align="center"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="执行结果"
        prop="status"
        align="center"
        width="100"
      >
        <template #default="scope">
          <y-dict-tag :options="tms_job_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column
        v-if="props.level === '0'"
        label="结果分析"
        prop="analysis"
        align="center"
      >
        <template #default="scope">
          <y-select
            v-model="scope.row.analysis"
            placeholder="请选择问题类型"
            :options="tms_tc_analysis"
          />
        </template>
      </el-table-column>
      <el-table-column
        v-if="props.level === '0'"
        label="备注"
        prop="remark"
        align="center"
      >
        <template #default="scope">
          <el-input type="textarea" v-model="scope.row.remark"></el-input>
        </template>
      </el-table-column>
    </el-table>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="submit">确定</el-button>
        <el-button @click="emit('close')">取消</el-button>
      </div>
    </template>
  </y-dialog>
</template>

<script setup name="Analysis">
import useUserStore from "@/store/modules/user";
const { proxy } = getCurrentInstance();

const emit = defineEmits(["submited", "close"]);

const { tms_job_status, tms_tc_analysis, tms_tc_result } = proxy.useDict(
  "tms_job_status",
  "tms_tc_analysis",
  "tms_tc_result"
);

const props = defineProps({
  data: { type: Array, default: undefined },
  level: { type: String, default: "0" },
  updateJobLog: { type: Function, default: undefined },
});

/** 提交按钮事件 */
function submit() {
  try {
    useUserStore()
      .getInfo()
      .then((res) => {
        props.data.forEach((val) => {
          val.analyser = res.data.user.id;
          props.updateJobLog(val).then(() => {
            proxy.$modal.msgSuccess("操作成功");
            submited();
          });
        });
      });
  } catch (err) {
    proxy.$modal.msgError(`操作失败，失败原因请参考：${err}`);
  }
}

/** submited事件 */
function submited() {
  emit("submited");
  emit("close");
}
</script>
